<%--
  Created by IntelliJ IDEA.
  User: 郭鸿宇
  Date: 2025/5/14
  Time: 10:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="/static/layui/css/layui.css" rel="stylesheet">
</head>
<body>
<form class="layui-form layui-row layui-col-space16">
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-username"></i>
            </div>
            <input type="text" name="A" value="" placeholder="Field A" class="layui-input" lay-affix="clear">
        </div>
    </div>
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <input type="text" name="B" placeholder="Field B" lay-affix="clear" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-date"></i>
            </div>
            <input type="text" name="C" readonly placeholder="Field C" class="layui-input demo-table-search-date">
        </div>
    </div>
    <div class="layui-btn-container layui-col-xs12">
        <button class="layui-btn" lay-submit lay-filter="demo-table-search">Search</button>
        <button type="reset" class="layui-btn layui-btn-primary">Clear</button>
    </div>
</form>

<table class="layui-hide" id="ID-table-demo-search"></table>

<script type="text/html" id="toolDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
    </div>
</script>

<table class="layui-hide" id="ID-table-demo-setRowChecked"></table>
<script type="text/html" id="demo-toolbar-setRowChecked">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>

<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script src="/static/js/jquery-1.8.3.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
    layui.use(function(){
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var upload = layui.upload ;
        // 创建表格实例
        table.render({
            toolbar: '#demo-toolbar-setRowChecked',
            elem: '#ID-table-demo-search',
            url: '/store/list', // 此处为静态模拟数据，实际使用时需换成真实接口
            cols: [[
                {type: 'checkbox', title: '😊', fixed: true}, // 单选框
                {field:'storeId', title: '主键id', width:120, sort: true, fixed: true},
                {field:'storeNum', title: '商品编号', width:120},
                {field:'communityName', title: '社区名称', width:120, sort: true},
                {field:'buildArea', title: '建筑面积', width:120},
                {field:'imgUrl', title: '图片', sort: true, fixed: true, templet: function (obj){
                    return '<img src="'+obj.imgUrl+'"/>'
                    }},
                {fixed: 'right', title:'操作', width: 134, minWidth: 125, templet: '#toolDemo'}
            ]],
            page: true,
            height: 310,
            request: {
                pageName: 'pageNum', // 页码的参数名称，默认：page
                limitName: 'pageSize' // 每页数据条数的参数名，默认：limit
            },
            parseData: function(res){ // res 即为原始返回的数据
                return {
                    "code": 0, // 解析接口状态
                    "msg": res.message, // 解析提示文本
                    "count": res.data.total, // 解析数据长度
                    "data": res.data.list // 解析数据列表
                };
            },
            limit:3
        });

        // 渲染
        upload.render({
            elem: '.demo-class-accept', // 绑定多个元素
            url: '/balanceRecharge/importFile', // 此处配置你自己的上传接口即可
            accept: 'file', // 普通文件
            done: function (res) {//res->Result
                layer.msg(res.msg);
                if (res.code == 200) {//成功
                    //刷新table
                    location.reload();
                }

                console.log(res);
            }
        });

        // 日期
        laydate.render({
            elem: '.demo-table-search-date',
            type: 'datetime'
        });
        // 搜索提交
        form.on('submit(demo-table-search)', function(data){
            var field = data.field; // 获得表单字段

            // 执行搜索重载
            table.reload('ID-table-demo-search', {
                page: {
                    curr: 1 // 重新从第 1 页开始
                },
                where: field // 搜索的字段
            });
            layer.msg('搜索成功<br>此处为静态模拟数据，实际使用时换成真实接口即可');
            return false; // 阻止默认 form 跳转
        });

        // 头工具栏事件
        table.on('toolbar(ID-table-demo-search)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            switch(obj.event){
                case 'add':
                    location.href="/owner/toAdd"
                    break;
            };
        });

        // 触发单元格工具事件
        table.on('tool(ID-table-demo-search)', function(obj){ // 双击 toolDouble
            var data = obj.data; // 获得当前行数据
            // console.log(obj)
            if(obj.event === 'edit'){
                layer.open({
                    title: '编辑 - id:'+ data.id,
                    type: 1,
                    area: ['80%','80%'],
                    content: '<div style="padding: 16px;">自定义表单元素</div>'
                });
            } else if(obj.event === 'del'){
                //删除之前先查询-判断有没有关联
                $.ajax({
                    url: '/store/selectOwner',
                    data:{sid:data.id},//商品ID
                    dataType: 'json',
                    success: function (resp) {//Result
                        //没有关联-ajax直接删
                        if(resp.code==200){
                            $.ajax({
                                url: '/store/toDelete',
                                data: JSON.stringify(data),
                                contentType: 'application/json',//前台给传参是json
                                type: 'post',
                                dataType: 'json',//后台给前端响应格式json
                                success: function (resp) { //Result
                                    alert(resp.msg);
                                    if(resp.code==200){
                                        local.reload();
                                    }
                                }
                            })
                        }else{//有关联-confirm弹框删
                            layer.confirm('该商铺下存在关联数据，确认删除吗？', function (index) {
                                // obj.del(); // 删除对应行（tr）的DOM结构
                                layer.close(index);
                                // 向服务端发送删除指令
                                $.ajax({
                                    url: '/store/doDelete',
                                    data: JSON.stringify(data),
                                    contentType: 'application/json',//前台给传参是json
                                    type: 'post',
                                    dataType: 'json',//后台给前端响应格式json
                                    success: function (resp) { //Result
                                        alert(resp.msg);
                                        if(resp.code==200){
                                            alert("删除成功")
                                            local.reload();
                                        }
                                    }
                                })

                            });
                        }


                    }
                })
            }
        });
    });
</script>

</body>
</html>